package controller

import (
	"apitestAfterGo/common"
	"apitestAfterGo/domain"
	"apitestAfterGo/dto"
	"apitestAfterGo/service"
	"apitestAfterGo/utils"
	"github.com/kataras/iris/v12/context"
	"github.com/kataras/iris/v12/mvc"
	"io/ioutil"
	"net/http"
	"net/url"
	"strings"
)

type TaskSuiteController struct {
}

func (t *TaskSuiteController) BeforeActivation(b mvc.BeforeActivation) {
	b.Handle("POST", "/getAllTaskSuiteByPage", "GetAllTaskSuiteByPage")
	b.Handle("POST", "/addSuite", "AddTaskSuite")
	b.Handle("GET", "/getTaskSuiteById/{id:int}", "GetTaskSuiteById")
	b.Handle("PUT", "/modifyTaskSuite", "ModifyTaskSuite")
	b.Handle("DELETE", "/deleteTaskSuiteById/{id:int}", "DeleteTaskSuiteById")
	b.Handle("POST", "/runTestCase", "RunTestCase")
}

func (t *TaskSuiteController) GetAllTaskSuiteByPage(dto dto.TaskSuiteInputDto) common.Result {
	taskSuites, count := service.TaskSuiteServiceImpl.GetAllTaskSuiteByPage(dto)
	pages := utils.GetPages(count, dto.PageSize)
	page := domain.Page{
		Pages:   pages,
		Total:   count,
		Current: dto.Current,
		Size:    dto.PageSize,
		Records: taskSuites,
	}
	return utils.Success(page, "获取测试套件成功")
}

func (t *TaskSuiteController) AddTaskSuite(context *context.Context, suite domain.TaskSuite) common.Result {
	username := utils.GetJwtParameter(context, "username").(string)
	suite.CreateBy = username
	suite.UpdateBy = username
	taskSuite := service.TaskSuiteServiceImpl.AddTaskSuite(suite)
	return utils.Success(taskSuite, "添加测试套件成功")
}

func (t *TaskSuiteController) GetTaskSuiteById(id int) common.Result {
	taskSuite := service.TaskSuiteServiceImpl.GetTaskSuiteById(id)
	return utils.Success(taskSuite, "获取当前测试套件成功")
}

func (t *TaskSuiteController) ModifyTaskSuite(context *context.Context, suite domain.TaskSuite) common.Result {
	username := utils.GetJwtParameter(context, "username").(string)
	suite.UpdateBy = username
	taskSuite := service.TaskSuiteServiceImpl.ModifyTaskSuite(suite)
	return utils.Success(taskSuite, "更新测试套件成功")
}

func (t *TaskSuiteController) DeleteTaskSuiteById(id int) common.Result {
	taskSuite := service.TaskSuiteServiceImpl.DeleteTaskSuiteById(id)
	return utils.Success(taskSuite, "删除测试套件成功")
}

func (t *TaskSuiteController) RunTestCase(body domain.RequestBody) common.Result {
	resp := HttpUtil(body)
	return resp
}

func HttpUtil(domain domain.RequestBody) common.Result {
	hc := http.Client{}
	params := formatParams(domain.Params)
	var body *strings.Reader
	if domain.Urlencodeds != nil {
		body = getBody(domain.Urlencodeds)
	}
	if domain.Json != nil {
		body = strings.NewReader(*domain.Json)
	}
	req, err := http.NewRequest(*domain.Method, *domain.Address+*domain.Path+params, body)
	if domain.Headers != nil {
		for i := range domain.Headers {
			req.Header.Set(domain.Headers[i].Key, domain.Headers[i].Value)
		}
	}
	resp, err := hc.Do(req)
	if err == nil {
		b, _ := ioutil.ReadAll(resp.Body)
		defer resp.Body.Close()
		return utils.Success(string(b), "请求接口成功")
	}
	return utils.Error(10000, "请求接口异常")
}

func getBody(encodes []*domain.Assert) *strings.Reader {
	if len(encodes) == 0 {
		return nil
	}
	body := url.Values{}
	for i := range encodes {
		body.Add(encodes[i].Key, encodes[i].Value)
	}
	return strings.NewReader(body.Encode())
}

func formatParams(params []*domain.Assert) string {
	if params == nil {
		return ""
	}
	ans := "?"
	for i := range params {
		if i == len(params)-1 {
			ans += params[i].Key + "=" + params[i].Value
		} else {
			ans += params[i].Key + "=" + params[i].Value + "&"
		}
	}
	return ans
}
